<template>
  <div class="my-container">
    <!-- 未登录头部模块 -->
    <div class="header not-login" v-if="user === null">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile" src="~@/assets/mobile.png" title="Login" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- 已登录头部模块 -->
    <div class="header user-info" v-else>
      <div class="base-info">
        <div class="left">
          <van-image round fit="cover" class="avatar" :src="userInfo.photo">
            <template v-slot:error>加载失败</template>
          </van-image>
          <span class="nickname">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button round size="small" type="default" to="/EditUserInfo"
            >编辑资料</van-button
          >
        </div>
      </div>
      <div class="data-states">
        <div class="data-box">
          <div class="count">{{ userInfo.art_count }}</div>
          <div class="text">头条</div>
        </div>
        <div class="data-box">
          <div class="count">{{ userInfo.art_count }}</div>
          <div class="text">关注</div>
        </div>
        <div class="data-box">
          <div class="count">{{ userInfo.fans_count }}</div>
          <div class="text">粉丝</div>
        </div>
        <div class="data-box">
          <div class="count">{{ userInfo.like_count }}</div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 宫格导航 -->
    <div class="bottom-nav">
      <van-cell title="消息通知" is-link to="#" />
      <van-cell title="小智同学" is-link to="#" />
    </div>
    <van-cell v-if="user" @click="quit" class="quit" is-link title="退出登录" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/my'
export default {
  name: 'my',
  data () {
    return {
      userInfo: {}
    }
  },
  methods: {
    quit () {
      this.$dialog
        .confirm({
          title: '退出登录',
          message: '亲，咱真退吗？'
        })
        .then(() => {
          this.$store.commit('delUser')
        })
        .catch(() => {
          // on cancel
        })
    },
    async getMyInfo () {
      this.$toast.loading('加载中')
      try {
        const { data: res } = await getUserInfo()
        this.userInfo = res.data
        this.$toast.clear()
      } catch (error) {
        this.$toast.fail('获取信息失败')
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    if (this.user) {
      this.getMyInfo()
    }
  }
}
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 361px;
    background: url("~@/assets/banner.png");
    background-size: cover;
  }
  .not-login {
    display: flex;
    align-items: center;
    justify-content: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .mobile {
        width: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      display: flex;
      justify-content: space-between;
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
      }
      .avatar {
        width: 132px;
        height: 132px;
        border: 2px solid #fff;
      }
      .nickname {
        font-size: 30px;
        color: #fff;
        margin-left: 23px;
      }
    }
    .data-states {
      display: flex;
      align-items: center;
      height: 130px;
      .data-box {
        flex: 1;
        color: #fff;
        text-align: center;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    .grid-item {
      height: 141px;
      i.iconfont {
        font-size: 45px;
      }
      .icon-shoucang {
        color: #eb5253;
      }
      .icon-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
  .bottom-nav {
    margin-top: 10px;
  }
  .quit {
    color: red;
    text-align: center;
    margin-top: 10px;
  }
}
</style>
